package com.shangbang.verb.controller.record;

import com.shangbang.verb.core.BaseResponse;
import com.shangbang.verb.service.record.UserRecordService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Api(value = "录音用户操作类接口", tags = "录音--用户相关接口", description = "录音用户的注册、更新")

@RestController
@RequestMapping(value = "/record/user")
public class UserRecordController {

    @Autowired
    private UserRecordService userRecordService;

    /**
     * 微信、手机注册
     * @param request
     * @param response
     * @param phone
     * @param nickname
     * @param gender
     * @param aboutMe
     * @param wxuid
     * @param icon
     * @param type
     * @return
     */
    @ApiOperation(value = "录音用户注册接口",notes = "手机号和微信均能注册")
    @ApiResponses({ @ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = -1, message = "操作失败") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = false, dataType = "String"),
            @ApiImplicitParam(name = "nickname", value = "昵称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "gender", value = "性别（0：男，1：女，2：未知）", required = true, dataType = "int"),
            @ApiImplicitParam(name = "aboutMe", value = "关于我", required = false, dataType = "String"),
            @ApiImplicitParam(name = "wxuid", value = "微信唯一标识", required = false, dataType = "String"),
            @ApiImplicitParam(name = "icon", value = "头像链接", required = false, dataType = "String"),
            @ApiImplicitParam(name = "type", value = "注册类型（type:0 手机号注册，type：1 微信注册）", required = true, dataType = "int")
    })
    @RequestMapping(value = "/createUser", method = {RequestMethod.POST, RequestMethod.GET})
    public BaseResponse createUser(HttpServletRequest request, HttpServletResponse response,
                                               @RequestParam(required = false) String phone,
                                               @RequestParam(required = true) String nickname,
                                               @RequestParam(required=true) Integer gender,
                                               @RequestParam(required=false) String aboutMe,
                                               @RequestParam(required = false) String wxuid,
                                               @RequestParam(required = false) String icon,
                                               @RequestParam(required = true) Integer type ) {
            Object result = "";
            Integer code = -1;
            String msg = "失败";
            //当手机号和微信同时为空的时候返回异常
            if (StringUtils.isBlank(phone)&&StringUtils.isBlank(wxuid)){
                return new BaseResponse(code, msg, result);
            }
            //判断手机号或者微信是否注册，如果已经注册则返回已有的信息
            Object state =userRecordService.getUserRecordState(phone,wxuid);
           //当返回是create字段的时候说明库里不存在数据，进行数据插入
            if ("create".equals(state)){

                 result =  userRecordService.createUserRecordByType(phone,nickname,gender,aboutMe,wxuid,icon,type);
                 if(!"".equals(result)||null!=result){
                     code = 0;
                     msg = "成功";
                 }

            }else{
                code = 0;
                msg = "成功";
                result=state;
            }
            return new BaseResponse(code, msg, result);
    }

   /* @RequestMapping(value = "/getCarouselList", method = RequestMethod.POST)
    public BaseResponse getCarouselList(HttpServletRequest request, HttpServletResponse response) {
        String result = "";
        Integer code = -1;
        String msg = "失败";

        result =  userService.createUserByType(phone,nickname,gender,aboutMe,wxuid,icon,type);


        return new BaseResponse(code, "查询列表成功", result);
    }*/

    /**
     * 修改用户信息
     * @param request
     * @param response
     * @param phone
     * @param nickname
     * @param gender
     * @param aboutMe
     * @param uid
     * @return
     */
    @ApiOperation(value = "录音修改用户信息接口",notes = "根据用户ID去操作")
    @ApiResponses({ @ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = -1, message = "操作失败") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = false, dataType = "String"),
            @ApiImplicitParam(name = "nickname", value = "昵称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "gender", value = "性别（0：男，1：女，2：未知）", required = true, dataType = "int"),
            @ApiImplicitParam(name = "aboutMe", value = "关于我", required = false, dataType = "String"),
            @ApiImplicitParam(name = "uid", value = "用户主键", required = true, dataType = "int")
    })
    @RequestMapping(value = "/updateUser", method = {RequestMethod.POST, RequestMethod.GET})
    public BaseResponse getCarouselList(HttpServletRequest request, HttpServletResponse response,
                                        @RequestParam(required = true) String phone,
                                        @RequestParam(required = false) String nickname,
                                        @RequestParam(required=false) Integer gender,
                                        @RequestParam(required=false) String aboutMe,
                                        @RequestParam(required = true) Integer uid) {
        Object result = "";
        Integer code = -1;
        String msg = "失败";

        result =  userRecordService.UpdateUserRecordByType(uid,phone,nickname,gender,aboutMe,"","");
        if (!result.equals("")){
            code = 0;
            msg = "成功";
        }

        return new BaseResponse(code, msg, result);

    }
    /**
     * 获取用户信息
     * @param request
     * @param response
     * @param uid
     * @return
     */
    @ApiOperation(value = "录音获取用户信息接口",notes = "根据用户ID获取")
    @ApiResponses({ @ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = -1, message = "操作失败") })
    @ApiImplicitParams({

            @ApiImplicitParam(name = "uid", value = "用户主键", required = true, dataType = "int")
    })
    @RequestMapping(value = "/getUserInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public BaseResponse getUserInfo(HttpServletRequest request, HttpServletResponse response,
                                        @RequestParam(required = true) Integer uid) {
        Object result = "";
        Integer code = -1;
        String msg = "失败";

        result =  userRecordService.getUserRecordInfo(uid);
        if (!result.equals("")){
            code = 0;
            msg = "成功";
        }

        return new BaseResponse(code, msg, result);

    }

}
